<?php

class ModificationController extends Zend_Controller_Action {

    private $_isUserAdmin = null;
    private $_userId = null;

    public function init() {
        if (Zend_Auth::getInstance()->hasIdentity()) {
            $validations = new Validations_User();
            $this->_userId = $validations->getUserId();
            $layout = $validations->getUserLayout();
            if ($layout == "admin") {
                $this->_isUserAdmin = true;
            }
            $this->_helper->layout->setLayout($layout);
        } else {
            $this->_redirect("/home");
        }
    }

    public function indexAction() {
        $projectId = $this->getRequest()->getParam("id");
        $projectDb = new Application_Model_Project();
        $project = $projectDb->getById($projectId);
        $detailsDb = new Application_Model_Details();
        $fetchAll = $detailsDb->fetchAll($projectId, 1);
        $this->view->fetchAll = $fetchAll;
        $this->view->isAdmin = $this->_isUserAdmin;
        $this->view->projectId = $projectId;
        $this->view->project = $project;
    }

    public function addAction() {
        $projectId = $this->getRequest()->getParam("id");
        $form = new Application_Form_Createnew();
        $form->submit->setLabel("Add New Modification");
        $this->view->form = $form;
        $this->view->projectId = $projectId;

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $optionsDb = new Application_Model_Options();
                $cloudinaryDetails = $optionsDb->getCloudianryDetails();
                $allImages = $_FILES['photo']['tmp_name'];
                $cloudinary = new Cloudinary_Main($cloudinaryDetails);
                $uploadResponse = array();
                foreach ($allImages as $image) {
                    $upload = $cloudinary->uploadImage($image);
                    array_push($uploadResponse, $upload);
                }
                $detail = array(
                    'title' => $values['title'],
                    'description' => $values['description'],
                    'images' => $uploadResponse,
                    'addedByAdmin' => $this->_isUserAdmin
                );
                $globalArray = array(
                    'main' => $detail,
                    'comments' => array()
                );
                $detailsDb = new Application_Model_Details();
                $detailsDb->createNew(array(
                    'project_id' => $projectId,
                    'details' => json_encode($globalArray),
                    'type' => 1,
                    'status' => 0
                ));
                $this->_redirect("/project/$projectId/modifications");
            }
        }
    }

    public function viewAction() {
        $projectId = $this->getRequest()->getParam("id");
        $modificationId = $this->getRequest()->getParam("modificationId");

        #get feature details
        $detailsDb = new Application_Model_Details();
        $modification = $detailsDb->getById($modificationId);
        $this->view->modification = $modification;
        $this->view->projectId = $projectId;

        #comment form
        $form = new Application_Form_Addcomment();
        $this->view->form = $form;

        #handle request
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $optionsDb = new Application_Model_Options();
                $cloudinaryDetails = $optionsDb->getCloudianryDetails();

                $cloudinary = new Cloudinary_Main($cloudinaryDetails);
                $uploadResponse = array();
                if ($values['photo'] != null) {
                    $allImages = $_FILES['photo']['tmp_name'];
                    foreach ($allImages as $image) {
                        $upload = $cloudinary->uploadImage($image);
                        array_push($uploadResponse, $upload);
                    }
                }

                $comment = array(
                    'description' => $values['description'],
                    'images' => $uploadResponse,
                    'addedByAdmin' => $this->_isUserAdmin
                );
                array_push($modification['details']['comments'], $comment);
                $detailsDb = new Application_Model_Details();
                $detailsDb->updateData(array('details' => json_encode($modification['details'])), $modificationId);
                $this->_redirect("/project/$projectId/modification/view/$modificationId");
            }
        }
    }

    public function statusAction() {
        $projectId = $this->getRequest()->getParam("id");
        $modificationId = $this->getRequest()->getParam("modificationId");
        $detailsDb = new Application_Model_Details();
        $modification = $detailsDb->getById($modificationId);
        $this->view->modification = $modification;
        $this->view->projectId = $projectId;

        #form
        $form = new Application_Form_Status();
        $optionDb = new Application_Model_Options();
        $modificationStatus = $optionDb->getByName("modificationStatus");
        $index = 0;
        foreach ($modificationStatus['details'] as $value) {
            $form->status->addMultiOption($index, $value);
            $index++;
        }
        $form->status->setValue($modification['status_id']);
        $form->id->setValue($modification['id']);
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $value = $form->getValues();
                $statusId = $value['status'];
                $detailsDb->updateData(array('status' => $statusId), $value['id']);
                $this->_redirect("/project/$projectId/modifications");
            }
        }
    }

}

